package com.zzh.ideas.web.aspect;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * description: <br>
 * createTime: 2019/8/228:48 <br>
 *
 * @author zzh
 */
@Component
@Aspect
@Slf4j
public class ServiceAspect {

    @Around(value = "cut()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        long start = System.currentTimeMillis();
        long end;
        try {
            Object result = joinPoint.proceed();
            end = System.currentTimeMillis();
//            log.info("执行" + joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName()
//                    + "方法," + parseParames(joinPoint.getArgs()) + ",耗时:" + (end - start) + " ms!");
            if((end - start) > 10_000) {
                log.error(joinPoint + ",service层耗时:" + (end - start) + " ms,");
            }
            return result;
        } catch (Throwable e) {
            end = System.currentTimeMillis();
            log.error(joinPoint + ",耗时:" + (end - start) + " ms,抛出异常 :" + e.getMessage());
            throw e;
        }
    }

    @Pointcut("execution(* com.zzh.ideas.web.service..*.*(..))")
    private void cut() {
    }

//    private String parseParames(Object[] parames) {
//
//        if (null == parames || parames.length <= 0) {
//            return "该方法没有参数";
//
//        }
//        StringBuffer param = new StringBuffer("请求参数 # 个:[ ");
//        int i = 0;
//        for (Object obj : parames) {
//            i++;
//            if (i == 1) {
//                param.append(obj.toString());
//                continue;
//            }
//            param.append(" ,").append(obj.toString());
//        }
//        return param.append(" ]").toString().replace("#", String.valueOf(i));
//    }
}
